package com.example.hotel.service;

import com.example.hotel.entity.Room;
import com.example.hotel.entity.Reservation;
import com.example.hotel.repo.RoomRepository;
import com.example.hotel.repo.ReservationRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class HotelService {
    private final RoomRepository roomRepo;
    private final ReservationRepository resRepo;

    public HotelService(RoomRepository roomRepo, ReservationRepository resRepo) {
        this.roomRepo = roomRepo;
        this.resRepo = resRepo;
    }

    // Rooms
    public Room createRoom(Room r){ return roomRepo.save(r); }
    public List<Room> listRooms(){ return roomRepo.findAll(); }
    public Room toggleAvailability(Long id, boolean available){
        Room r = roomRepo.findById(id).orElseThrow();
        r.setAvailable(available);
        return roomRepo.save(r);
    }

    // Reservations
    public Reservation createReservation(Reservation r){
        if (!r.getRoom().isAvailable()) throw new IllegalStateException("Room not available");
        return resRepo.save(r);
    }
    public List<Reservation> listReservations(){ return resRepo.findAll(); }
}
